package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * 订购数量属性校验
 */
public class QuantityOrderedAttrHandler extends AbstractAttrHandler<KiteProdInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void before(KiteAttr attr, KiteProdInst inst, String value, AttrData data) {
        //模版还原时，不做校验
        if ("sceneTempl".equals(data.getExtAttributes().get("entry"))) {
            return;
        }
        //变更业务的场景初始化过程中不校验
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))) {
            return;
        }
        if (KiteStringUtils.isEqual("200001696", attr.getFieldName())) { //订购数量
            KiteAttr kiteAttr2 = inst.getAttr("200003677"); //合同约定开卡数量
            KiteAttr kiteAttr = inst.getAttr("200003992"); //合同约定订购数量
            int contractNum = 0;
            if (!KiteObjectUtils.isEmpty(kiteAttr) && KiteStringUtils.isNotEmpty(kiteAttr.getValue())) {
                contractNum = Integer.parseInt(kiteAttr.getValue());
            }
            if (contractNum == 0 && !KiteObjectUtils.isEmpty(kiteAttr2) && KiteStringUtils.isNotEmpty(kiteAttr2.getValue())) {
                contractNum = Integer.parseInt(kiteAttr2.getValue());
            }
            if (value == null) {
                return;
            }
            int valueNum = Integer.parseInt(value);
            if (valueNum > contractNum) {
                BssException.throwOut("'订购数量'不允许大于'合同约定订购数量'", "'订购数量'不允许大于'合同约定订购数量'", "'订购数量'不允许大于'合同约定订购数量'");
            //if (valueNum >= contractNum) {
            //    BssException.throwOut(KeyConsts.LOG_MODULE, "-1", "订购数量必须小于合同约定订购数量");
            }
        }
        else { //合同约定订购数量
            KiteAttr kiteAttr = inst.getAttr("200001696"); //订购数量
            if (KiteObjectUtils.isEmpty(kiteAttr) || KiteStringUtils.isEmpty(kiteAttr.getValue())) {
                return;
            }
            int valueNum = 0;
            if (!KiteObjectUtils.isEmpty(kiteAttr) && KiteStringUtils.isNotEmpty(kiteAttr.getValue())) {
                valueNum = Integer.parseInt(kiteAttr.getValue());
            }
            if (value == null) {
                return;
            }
            int contractNum = Integer.parseInt(value);
            if (valueNum > contractNum) {
                BssException.throwOut("'订购数量'不允许大于'合同约定订购数量'", "'订购数量'不允许大于'合同约定订购数量'", "'订购数量'不允许大于'合同约定订购数量'");
            //if (valueNum >= contractNum) {
            //    BssException.throwOut(KeyConsts.LOG_MODULE, "-1", "订购数量必须小于合同约定订购数量");
            }
        }

    }

    @Override
    public void after(KiteAttr attr, KiteProdInst inst, AttrData data) {

        //产品变更 订购数量 置灰
        //4G定制化连接
        //4G定制连接产品-Cat.1
        //CAT1通用连接产品
        if (KiteStringUtils.isEqual(data.getServiceOfferId(), "2826")
            && ((KiteStringUtils.isEqual(attr.getFieldName(), "200001696") && KiteStringUtils.equalsAny(inst.getSpecId(), ProdCateUtil.getMainMemberProdIdForArr(ProdCateUtil.GIFT_DZ4G_PROD, ProdCateUtil.GIFT_DZCAT1_PROD, ProdCateUtil.GIFT_TYCAT1_PROD)))
            || (KiteStringUtils.equals(inst.getSpecId(), ProdCateUtil.getMainMemberProdId(ProdCateUtil.GIFT_DZ5G_PROD))))) {
            attr.setIsEdit(KeyConsts.IFTRUE_F);
            operateSceneInstService
                .setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
        }
        // 作为融合子成员时，订购数量都是置灰不可编辑，该类只能作为子成员的订购数量配置
        KiteOfferInst giftOfferInst = querySceneInstService.getGiftOfferInst(data.getSceneInstId());
        if (Objects.nonNull(giftOfferInst) && KiteStringUtils.equals(attr.getFieldName(), "200001696")) {
            attr.setIsEdit(KeyConsts.IFTRUE_F);
        }
        super.after(attr, inst, data);
    }
}
